using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


namespace Dungeon_Dive
{
    /// <summary>
    /// This is a game component responsible for drawing status information on the screen.
    /// </summary>
    public class HealthBar : DrawableGameComponent
    {

        Game parentGame;

        Log log;

        SpriteBatch sb;
        SpriteFont font;
        Texture2D sprites;
        Vector2 barPosition;
        Rectangle barFrame;
        Vector2 attackTextPosition;
        Vector2 attackIconPosition;
        String attackText;

        Vector2[] slots;
        Rectangle[] elems;

        Elements[] tokens;

        int attackType;

        public HealthBar(Game game)
            : base(game)
        {
            parentGame = game;
            sprites = parentGame.Content.Load<Texture2D>("Textures//sprites");            
            log = (Log)game.Services.GetService(typeof(Log));

            barPosition = new Vector2((float)Game.Window.ClientBounds.Width - 320,
                                        (float)Game.Window.ClientBounds.Height - 80);
            barFrame = new Rectangle(23, 23, 307, 67);

            slots = new Vector2[5];
            slots[0] = new Vector2(barPosition.X + 8, barPosition.Y + 8);
            slots[1] = new Vector2(barPosition.X + 67, barPosition.Y + 8);
            slots[2] = new Vector2(barPosition.X + 128, barPosition.Y + 8);
            slots[3] = new Vector2(barPosition.X + 189, barPosition.Y + 8);
            slots[4] = new Vector2(barPosition.X + 248, barPosition.Y + 8);

            elems = new Rectangle[4];
            elems[(int)Elements.Earth] = new Rectangle(25, 200, 50, 50);
            elems[(int)Elements.Water] = new Rectangle(100, 200, 50, 50);
            elems[(int)Elements.Air] = new Rectangle(175, 200, 50, 50);
            elems[(int)Elements.Fire] = new Rectangle(250, 200, 50, 50);

            tokens = new Elements[0];

            attackText = "Attack type:";
            attackTextPosition = new Vector2(barPosition.X + 80, barPosition.Y - 70);
            attackIconPosition = new Vector2(barPosition.X + 248, barPosition.Y - 75);
            attackType = -1;            
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            base.Initialize();
        }

        /// <summary>
        /// Loads content for HealthBar
        /// </summary>
        protected override void LoadContent()
        {
            log.Write("Healthbar.LoadContent()");
            ContentManager contentManager = (ContentManager)parentGame.Services.GetService(typeof(ContentManager));
            sb = contentManager.SpriteBatch;
            if (sb == null) log.Write("Healthbar load SpriteBatch failed");
            font = contentManager.Font;            
            base.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if (sb == null)
                log.Write("Healthbar has no SpriteBatch");
            if (sprites == null)
                log.Write("Healthbar has no picture");

            sb.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.BackToFront,
                SaveStateMode.SaveState);
            sb.Draw(sprites, barPosition, barFrame, Color.White);
            for (int i = 0; i < tokens.Length; ++i)
            {
                sb.Draw(sprites, slots[i], elems[(int)tokens[i]], Color.White);                
            }
            if (attackType != -1)
            {                                               
                sb.DrawString(font, attackText, attackTextPosition, Color.White, 0f, Vector2.Zero, 1.5f, SpriteEffects.None, 0f);
                sb.Draw(sprites, attackIconPosition, elems[attackType], Color.White);
            } 
            sb.End();
            base.Draw(gameTime);
        }

        /// <summary>
        /// Used to pass playercharacter's status information to HealthBar instance.
        /// </summary>
        /// <param name="tokenList">List of playercharacter's elemental tokens</param>
        /// <param name="attackType">Type of playercharacter's attack</param>
        public void SetHealthbar(List<Elements> tList, int aType)
        {
            tList.Reverse();
            tokens = new Elements[tList.Count];
            tList.CopyTo(tokens);
            attackType = aType;
        }

        // Really, should have a listener for health change events rather than polling every round

    }
}